# -*- coding: utf-8 -*-
from db.mysql import MysqlClient
import requests
from itertools import product
from common.helper import Helper
from common.config import *
import json


class Arrangement(object):

    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "空中一号航空意外伤害保险"
    #生成产品信息
    def genProductInfo(self):

        plan = {
            "planA":{
                "days": ["30m"],
                "object":[{"airplane_death":"5000000"}]
            },
            "planB":{
                "days":["1y"],
                "object":[
                    {"airplane_death": "5000000"},
                    {"airplane_death": "10000000"},
                    {"airplane_death": "15000000"},
                    {"airplane_death": "20000000"},
                ]
            }

        }
        prices = ["9.9","99","188","278","365"]
        # 计数变量
        num = 0
        for k in plan:

            for v in product(plan[k]["days"],plan[k]["object"]):

                data = {
                    "name":self.name,
                    "company":"华夏保险",
                    "days":v[0],
                    "people_count":"1",
                    "price":Helper.decimalParse(prices[num])

                }
                data.update( v[1])
                self.mergeTable( data )
                num += 1

    # 合并表格
    def mergeTable(self, data):

        SQL = 'INSERT INTO `' + TABLE_ACCIDENT + '`(`name`,`company`,`days`,`people_count`,`price`,`airplane_death`)VALUES("{name}","{company}","{days}","{people_count}","{price}","{airplane_death}")'

        print(SQL.format(**data))

        self.mysql.cursor.execute(SQL.format(**data))
        self.mysql._conn.commit()


if __name__ == '__main__':

    a = Arrangement()
    a.genProductInfo()